/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_03_phi_vs_z_rca.do
Date: October 2022

Description: Reproduces Table 3: Sectoral firm-embedded productivity and comparative advantage.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/table_03_phi_vs_z_rca.log",replace
*-------------------------------------------------------------------------------


*-----------------------------------------------------------------------------	
*Output per worker 
*-----------------------------------------------------------------------------
use "${data}/aggregates_tfp_lp_klems_agg.dta", clear 
keep year isocode ln_ctfp ln_gdp_pw_ppp 
duplicates drop 
tempfile base0
save `base0', replace 

use "${data}/aggregates_tfp_lp_klems_agg.dta", clear 
keep year isocode type ln_LP_ppp_emp_pwt
rename ln_LP_ppp_emp_pwt ln_lp_
reshape wide ln_lp_ , i( year isocode ) j(type) string
duplicates drop 
tempfile base1
save `base1', replace 

use `base0', clear
merge 1:1 isocode year using `base1'
drop _merge 
keep if year==2016 
tempfile productivity_agg
save `productivity_agg', replace 

use "${data}/aggregates_tfp_lp_klems.dta", clear 
keep year isocode type sector1 sector ln_LP_ppp_emp_pwt
rename ln_LP_ppp_emp_pwt ln_lp_sec
duplicates drop 
merge m:1 isocode year using `productivity_agg'
keep if _merge==3
drop _merge
tempfile productivity
save `productivity', replace 

	
		
*-----------------------------------------------------------------------------
*Outward Multinational*
*-----------------------------------------------------------------------------
use "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep year isocode  sector1 sector GO_usd_mnc_out
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
keep if year==2016
drop if isocode=="KR" | isocode=="MX" 

gen GO_manuf= GO_usd_mnc_out if sector1=="Manufacturing (C)"
gen GO_serv= GO_usd_mnc_out if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
gen GO_tot= GO_usd_mnc_out if sector1=="Total (TOT)"
gen GO_market= GO_usd_mnc_out if sector1=="Market (MARKT)"
drop if sector1=="Non-Market Economy"
rename GO_usd_mnc_out GO_sec

gen GO_sec_fr=GO_sec if isocode=="FR"
by year sector, sort: egen xx_max=max(GO_sec_fr)
gen ln_ratio=ln(GO_sec/xx_max)
drop GO_sec_fr GO_sec xx_max
rename ln_ratio GO_sec

foreach ii in GO_manuf GO_serv GO_tot GO_market {
display "`ii'"
by year isocode, sort: egen xx=max(`ii')
drop `ii'
rename xx `ii'

gen `ii'_fr=`ii' if isocode=="FR"
by year, sort: egen xx_max=max(`ii'_fr)
gen ln_ratio=ln(`ii'/xx_max)
drop `ii'_fr `ii' xx_max
rename ln_ratio `ii'
}
*

drop if sector==sector1
merge 1:1 isocode year sector using `productivity'
drop if _merge==2
drop _merge
tempfile out_mnc
save `out_mnc', replace 


*Only aggregates* 
*------------------------------------------
use `out_mnc', clear
keep year isocode  GO_manuf GO_serv GO_tot GO_market
duplicates drop
merge 1:1 isocode year using `productivity_agg'
drop if _merge==2
drop _merge
tempfile out_mnc_sec
save `out_mnc_sec', replace



*-----------------------------------------------------------------------------
*PHI*
*-----------------------------------------------------------------------------
local nn=10 
*minimum number of sectors allowed per year-isocode*/
local yy=2016
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear
keep if year==`yy'
rename DA D_A
drop if D_A==. | (D_A==0 & isocode!="FR")
tab type

keep isocode year type sector1 sector D_A
rename D_A D_A_sec
merge m:1 year isocode using "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", keepusing(D_A)
rename D_A D_A_agg
drop if _merge==2
drop _merge
merge m:1 year isocode type using "${data}/estimates_aggtype_naics_sales_s1_base_woparent.dta", keepusing(D_A)
rename D_A D_A_type
drop if _merge==2
drop _merge

gen D_A_manuf=D_A_type if sector1=="Manufacturing (C)"
gen D_A_serv=D_A_type if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
gen D_A_others=D_A_type if sector1=="Other_Goods (A-B-D-E-F)"
rename D_A_agg D_A_tot
drop D_A_type

foreach ii in D_A_manuf D_A_serv D_A_others  {
display "`ii'"
by year isocode, sort: egen xx=max(`ii')
drop `ii'
rename xx `ii'
}
*

*Bring MNC information
*-------------------------------- 
merge 1:1 year isocode sector using `out_mnc'
keep if _merge==3
drop _merge

gen D_A_tot_phi=(-1)*D_A_tot*(0.2)
gen b_D_A_tot_phi_`grvar0'=ln_gdp_pw_ppp - D_A_tot_phi 

gen D_A_manuf_phi=(-1)*D_A_manuf*(0.2)
gen b_D_A_manuf_phi_`grvar0'=ln_lp_manuf - D_A_manuf_phi

gen D_A_serv_phi=(-1)*D_A_serv*(0.2)
gen b_D_A_serv_phi_`grvar0'=ln_lp_serv - D_A_serv_phi

gen D_A_sec_phi=(-1)*D_A_sec*(0.2)
gen b_D_A_sec_phi_`grvar0'=ln_lp_sec - D_A_sec_phi

drop D_A_sec D_A_tot D_A_manuf D_A_serv D_A_others 
tempfile table3
save `table3', replace



*-----------------------------------------------------------------------------
*Table 3
*-----------------------------------------------------------------------------
use `table3', clear
local tt tot
gen rhs1=D_A_sec_phi- D_A_`tt'_phi
gen rhs2=b_D_A_sec_phi- b_D_A_`tt'_phi
gen lhs= GO_sec-GO_`tt'

local LHS lhs 
local RHS1 rhs1
local RHS2 rhs2
drop if `LHS'==.

gen ss=1
local abslist "ss"

*All sectors
*-------------------------------------------------------------
reghdfe `LHS' `RHS1', abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "all sectors") append 
reghdfe `LHS' `RHS2', abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "all sectors") append 
reghdfe `LHS' `RHS1' `RHS2', abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "all sectors") append 

*Manufacturing 
*-------------------------------------------------------------
reghdfe `LHS' `RHS1' if type=="manuf", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "manuf") append 
reghdfe `LHS' `RHS2' if type=="manuf", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "manuf") append 
reghdfe `LHS' `RHS1' `RHS2' if type=="manuf", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "manuf") append 

*Services 
*-------------------------------------------------------------
reghdfe `LHS' `RHS1' if type=="serv", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "serv") append 
reghdfe `LHS' `RHS2' if type=="serv", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "serv") append 
reghdfe `LHS' `RHS1' `RHS2' if type=="serv", abs(`abslist')  vce(cluster isocode sector)
outreg2 using "${rmain}/table_03.xls", br ctitle(OLS, type: "serv") append 
*------------------------------------

log close 
